package com.copa.module.system.service.impl;

import com.copa.domain.ResponseDto;
import com.copa.module.order.service.OrderService;
import com.copa.module.system.domain.dto.TradeDto;
import com.copa.module.system.domain.entity.TradeEntity;
import com.copa.module.system.domain.vo.TradeVo;
import com.copa.module.system.mapper.TradeDao;
import com.copa.module.system.service.TradeService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author copa
 * @createDate 2021-02-26 14:00
 * @function
 */
@Service
public class TradeServiceImpl implements TradeService {

    @Autowired
    private TradeDao tradeDao;

    @Autowired
    private OrderService orderService;

    @Override
    public ResponseDto<List<TradeDto>> chart(TradeVo tradeVo) {
        LocalDate startTime = tradeVo.getDateRange().get(0);
        LocalDate endTime = tradeVo.getDateRange().get(1);
        List<TradeDto> tradeEntities = tradeDao.findTradeByDays(startTime, endTime, tradeVo.getType());
        return ResponseDto.success(tradeEntities);
    }

    @Override
    public Long findAddTotalOrderNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findAddTotalOrderNumByTime(now, yesterday);
    }

    @Override
    public Long findAddOrderGoodsNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findAddOrderGoodsNumByTime(now, yesterday);
    }

    @Override
    public Long findAddPayTotalOrderNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findAddPayTotalOrderNumByTime(now, yesterday);
    }

    @Override
    public Long findAddPayOrderGoodsNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findAddPayOrderGoodsNumByTime(now, yesterday);
    }

    @Override
    public Long findRefundOrderGoodsNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findRefundOrderGoodsNumByTime(now, yesterday);
    }

    @Override
    public BigDecimal findIncomeByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findIncomeByTime(now, yesterday);
    }

    @Override
    public BigDecimal findRefundByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findRefundByTime(now, yesterday);
    }

    @Override
    public Long findAddSaleGoodsNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findAddSaleGoodsNumByTime(now, yesterday);
    }

    @Override
    public Long findRefundSaleGoodsNumByTime() {
        LocalDate now = LocalDate.now();
        LocalDate yesterday = now.minusDays(1);
        return orderService.findRefundSaleGoodsNumByTime(now, yesterday);
    }

    @Override
    public void insert(TradeEntity trade) {
        tradeDao.insert(trade);
    }
}
